<?php
class User_Actor_model extends grocery_CRUD_Model
{
	 function get_list()
    {
    	if($this->table_name === null)
    		return false;
    	
    	$select = "{$this->table_name}.*";
		$select .= ", users.username, users.password";
		
    	if(!empty($this->relation))
    		foreach($this->relation as $relation)
    		{
    			list($field_name , $related_table , $related_field_title) = $relation;
    			$unique_join_name = $this->_unique_join_name($field_name);
    			$unique_field_name = $this->_unique_field_name($field_name);
    			
				if(strstr($related_field_title,'{'))
    				$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
    			else    			
    				$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
    			
    			if($this->field_exists($related_field_title))
    				$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
    		}
    		
    	$this->db->select($select, false);
    	
		$this->db->join('users','users.actor_id = '. $this->table_name . '.id');
		$this->db->where('users.id', $_SESSION['id']);
		
    	$results = $this->db->get($this->table_name)->result();
    	return $results;
    }
	
	function get_edit_values($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	$this->db->where('users.' . $primary_key_field,$primary_key_value);
		$this->db->join('users','users.actor_id = ' . $this->table_name . '.id');
    	$result = $this->db->get($this->table_name)->row();
		//echo $this->db->last_query();
		$result->password = '';
    	return $result;
    }

	function db_update($post_array, $primary_key_value)
    {
		$data_user['username'] = $post_array["username"];
		if(!empty($post_array['password']))
			$data_user['password'] = md5($post_array['password']);
			
		$this->db->update('users', $data_user, array('id' => $_SESSION['id']));
    }
	
}